package com.gxa.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.common.LayR;
import com.gxa.common.R;
import com.gxa.mapper.SatisfactionMapper;
import com.gxa.mapper.ValueMapper;
import com.gxa.mapper.customerMapper;
import com.gxa.param.AddCusatis;
import com.gxa.param.MyTableParam;
import com.gxa.pojo.*;
import com.gxa.service.customerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

@Service
@Transactional(rollbackFor = Throwable.class)
public class customerServiceImpl implements customerService {
    @Autowired
    private customerMapper customerMapper;
    @Autowired
    private SatisfactionMapper satisfactionMapper;

    @Autowired
    private ValueMapper valueMapper;
    //返回客户价值列表
    @Override
    public LayR list(MyTableParam param) {
        PageHelper.startPage(param.getPage(),param.getLimit());
        List<Customer> customers=customerMapper.findValueAll(param);
        System.out.println("---------------"+customers);
        PageInfo<Customer> customerPageInfo = new PageInfo<>(customers);
        return new LayR(0,"success",customerPageInfo.getList(),customerPageInfo.getTotal());
    }
    //返回客户满意度列表
    @Override
    public LayR satislist(MyTableParam param) {
        PageHelper.startPage(param.getPage(),param.getLimit());
        List<Customer> customers=customerMapper.findSatisfactionAll(param);
        PageInfo<Customer> customerPageInfo = new PageInfo<>(customers);
        return new LayR(0,"success",customerPageInfo.getList(),customerPageInfo.getTotal());
    }
    //返回客户类型列表
    @Override
    public LayR typelist() {
        List<Type> types=customerMapper.findtypeAll();
        return new LayR(0,"success",types);
    }
//返回顾客历史订单
    @Override
    public LayR orderlist(Integer customerId,MyTableParam param) {
        PageHelper.startPage(param.getPage(),param.getLimit());
        List<OrderSelect> orderSelects=customerMapper.findBycustID(customerId);
        PageInfo<OrderSelect> orderSelectPageInfo = new PageInfo<>(orderSelects);
        return new LayR(0,"success",orderSelectPageInfo.getList(),orderSelectPageInfo.getTotal());

    }

    @Override
    public R deleteOne(Integer customerId,Integer satisId) {

        customerMapper.deleteOneById(satisId);
        satisfactionMapper.updateSatisId(customerId);
        return new R(200,"删除成功");
    }

    @Override
    public R add(AddCusatis addCusatis, HttpSession session) {

        System.out.println(addCusatis);
        //添加信誉度
        Value value=new Value();
        value.setConsumptionTimes(addCusatis.getConsumptionTimes());
        value.setTotalConsumption(addCusatis.getTotalConsumption());
        value.setValueLevel(addCusatis.getValueLevel());
        value.setReputationLevel(addCusatis.getReputationLevel());
        value.setCareProgram("A");
        value.setServiceRepresentative("admin");
        //调用mapper
        valueMapper.add(value);

        //添加满意度
        Satisfaction satisfaction=new Satisfaction();
        satisfaction.setQualitySatisfaction(addCusatis.getQualitySatisfaction());
        satisfaction.setServiceSatisfaction(addCusatis.getServiceSatisfaction());
        satisfaction.setTotalSatisfaction(addCusatis.getTotalSatisfaction());
        satisfaction.setCosteffectiveSatisfaction(addCusatis.getCosteffectiveSatisfaction());
        satisfaction.setFeedback(addCusatis.getFeedback());
        Date date=new Date();
        satisfaction.setCreateTime(new java.sql.Timestamp(date.getTime()));
        //调用Mapper
        satisfactionMapper.add(satisfaction);

        Customer customer1=customerMapper.findbycustId(addCusatis.getCustomerId());
        Customer customer=new Customer();
        customer.setCustomerName(addCusatis.getCustomerName());
        customer.setCustomerGender(addCusatis.getCustomerGender());
        customer.setCustomerStatus(addCusatis.getCustomerStatus());
        customer.setCustomerStateAssignment(customer1.getCustomerStateAssignment());
        Date date2=new Date();
        customer.setCreateTime(new java.sql.Timestamp(date2.getTime()));
        customer.setEntryPerson((String) session.getAttribute("emplName"));
        System.out.println((String) session.getAttribute("emplName"));
        customer.setValueId(value.getId());
        customer.setSatisfactionId(satisfaction.getId());
        customer.setTypeId(addCusatis.getTypeId());

        //调用mapper
        customerMapper.add(customer);
        return new R(200,"添加成功");

    }

    @Override
    public R findById(Integer customerId) {
        Customer customer = customerMapper.findbycustId(customerId);
        return new R(200,"success",customer);
    }

    @Override
    public R findfromByID(Integer customerId) {
        Customer customer=customerMapper.selectById(customerId);
        return new R(200,"success",customer);
    }

    @Override
    public R edit(AddCusatis addCusatis, HttpSession session) {
        System.out.println(addCusatis);
        //添加信誉度
        Value value=new Value();
        value.setId(addCusatis.getValueId());
        System.out.println("-----------"+addCusatis.getValueId());
        value.setConsumptionTimes(addCusatis.getConsumptionTimes());
        //调用mapper
        valueMapper.update(value);
        //添加满意度
        Satisfaction satisfaction=new Satisfaction();
        satisfaction.setQualitySatisfaction(addCusatis.getQualitySatisfaction());
        satisfaction.setServiceSatisfaction(addCusatis.getServiceSatisfaction());
        satisfaction.setTotalSatisfaction(addCusatis.getTotalSatisfaction());
        satisfaction.setCosteffectiveSatisfaction(addCusatis.getCosteffectiveSatisfaction());
        satisfaction.setFeedback(addCusatis.getFeedback());
        Date date=new Date();
        satisfaction.setCreateTime(new java.sql.Timestamp(date.getTime()));
        satisfaction.setId(addCusatis.getSatisId());
        System.out.println(addCusatis.getSatisId());
        //调用Mapper
        satisfactionMapper.update(satisfaction);



        Customer customer1=customerMapper.findbycustId(addCusatis.getCustomerId());
        Customer customer=new Customer();
        customer.setCustomerGender(addCusatis.getCustomerGender());
        customer.setCustomerStatus(addCusatis.getCustomerStatus());
        customer.setCustomerStateAssignment(customer1.getCustomerStateAssignment());
        Date date2=new Date();
        customer.setCreateTime(new java.sql.Timestamp(date2.getTime()));
        customer.setEntryPerson((String) session.getAttribute("emplName"));
        customer.setCustomerId(addCusatis.getCustomerId());
//调用Mapper
        customerMapper.update(customer);
        return new R(200,"修改成功");
    }




//    顾客部分
    @Override
    public LayR Culist(MyTableParam param) {
        PageHelper.startPage(param.getPage(),param.getLimit());

        List<Customer> customers  = customerMapper.findAll(param);

        PageInfo<Customer> emplPageInfo = new PageInfo<>(customers);


        return new LayR(0,"success",emplPageInfo.getList(),emplPageInfo.getTotal());
    }

    @Override
    public R deleteCuOne(Integer id) {
        customerMapper.deleteCuOneById(id);
        return new R(200,"删除成功");
    }

    @Override
    public R deleteAll(String[] ids) {
        customerMapper.deleteAll(ids);
        return new R(200,"删除成功");

    }

    @Override
    public R addCu(Customer customer, HttpSession session) {
        customer.setEntryPerson((String) session.getAttribute("emplName"));
        customer.setCreateTime(new Timestamp(System.currentTimeMillis()));
        customer.setUpdateTime(new Timestamp(System.currentTimeMillis()));
        customerMapper.save(customer);
        return new R(200,"添加成功");
    }

    @Override
    public R findCuById(Integer id) {
        Customer byPk = customerMapper.findCuByPk(id);

        return new R(200,"success",byPk);
    }

    @Override
    public R editCu(Customer customer, HttpSession session) {
        customer.setEntryPerson((String) session.getAttribute("emplName"));

        customer.setUpdateTime(new Timestamp(System.currentTimeMillis()));

        customerMapper.upCudate(customer);

        return new R(200,"修改成功");
    }

    @Override
    public R edit1(Customer customer, HttpSession session) {
        customer.setEntryPerson((String) session.getAttribute("emplName"));

        customer.setCustomerStateAssignment(1);

        customer.setUpdateTime(new Timestamp(System.currentTimeMillis()));

        customerMapper.upCudate(customer);

        return new R(200,"修改成功");

    }

}
